package com.imay.live.activity.profit;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.imay.live.activity.BaseRequestActivity;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.view.IncomeHistView;
import com.imay.live.model.ProHistoryViewModel;
import com.imay.live.R;

import static com.imay.live.proto.ResultCode.CodeSuccess;

/**
 * Created by lzx on 2017/04/17 0017.
 * 魅力购买 兑换(魅力->魅钻)
 */

public class ProfitHistoryActivity extends BaseRequestActivity {
    LinearLayout src_linear;

    public static void startActivity(Context context) {
        Intent starter = new Intent(context, ProfitHistoryActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.profithistory_layout;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setTitle(getString(R.string.profitmoney));
        src_linear = $(R.id.src_linear);
//        TextView rightTextView = getTitleBar().addRightTextView(getString(R.string.getprofitmoney));
//        rightTextView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });

        loadData();
    }

    void initData(ProHistoryViewModel proHistoryViewModel) {
        if (proHistoryViewModel == null) {
            return;
        }
        LineView lineView = new LineView(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.item_top));
        src_linear.addView(lineView, params);
        int left = (int) getResources().getDimension(R.dimen.item_left);
        setLine(left, getResources().getString(R.string.income_name), proHistoryViewModel.getUser().getNick(), "");
        setLine(getResources().getString(R.string.income_ID), String.valueOf(proHistoryViewModel.getUser().getRoomId()), "");
        LineView lineView1 = new LineView(this);
        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.item_mid));
        src_linear.addView(lineView1, params1);

        setLine(left, getResources().getString(R.string.income_level), "LV" + proHistoryViewModel.getUser().getLiveLevel(), "");
        setLine(left, getResources().getString(R.string.income_meili), String.valueOf(proHistoryViewModel.getMeili()), "");
        String accountStatus;
        if (proHistoryViewModel.getAccountStatus() == 0) {
            accountStatus = getResources().getString(R.string.income_narmol);
        } else if (proHistoryViewModel.getAccountStatus() == 1) {
            accountStatus = getResources().getString(R.string.income_nobuf);
        } else if (proHistoryViewModel.getAccountStatus() == 2) {
            accountStatus = getResources().getString(R.string.income_noroom);
        } else {
            accountStatus = getResources().getString(R.string.income_noNuber);
        }
        setLine(left, getResources().getString(R.string.income_nowstatus), accountStatus, "");
        setLine(getResources().getString(R.string.income_setstatus), proHistoryViewModel.getWithdrawStatus() == 0 ? getResources().getString(R.string.income_narmol) : getResources().getString(R.string.income_no), "");

        LineView lineView2 = new LineView(this);
        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.item_mid));
        src_linear.addView(lineView2, params2);
        setLine(left, getResources().getString(R.string.income_mongift), String.valueOf(proHistoryViewModel.getGiftIncome()), "--");
        setLine(left, getResources().getString(R.string.income_montask), String.valueOf(proHistoryViewModel.getTaskIncome()), "--");
        setLine(left, getResources().getString(R.string.income_monactivity), String.valueOf(proHistoryViewModel.getActivityIncome()), "--");
        setLine(left, getResources().getString(R.string.income_monsum), String.valueOf(proHistoryViewModel.getTotalIncome()), "--");
        setLine(left, getResources().getString(R.string.income_follower), String.valueOf(proHistoryViewModel.getFollower()), "--");
        setLine(left, getResources().getString(R.string.income_rank), String.valueOf(proHistoryViewModel.getLiveRank()), "--");
        setLine(getResources().getString(R.string.income_vio), String.valueOf(proHistoryViewModel.getViolation()), "--");
        ;
    }

    class LineView extends View {
        Paint paint;
        Path path;

        public LineView(Context context) {
            super(context);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            if (path == null) {
                path = new Path();
                paint = new Paint();
                paint.setStyle(Paint.Style.STROKE);
                paint.setAntiAlias(true);
                paint.setStrokeWidth(2);
                paint.setColor(getResources().getColor(R.color.line_ec));
            }
            path.reset();
            path.moveTo(0, canvas.getHeight() - paint.getStrokeWidth());
            path.lineTo(canvas.getWidth(), canvas.getHeight() - paint.getStrokeWidth());
            canvas.drawPath(path, paint);
        }
    }

    private void setLine(int left, String name, String content, String end) {
        IncomeHistView ih_name = new IncomeHistView(this, left);
        ih_name.setName(name);
        ih_name.setContent(content);
        ih_name.setEnd(end);
        src_linear.addView(ih_name);
    }

    private void setLine(String name, String content, String end) {
        setLine(0, name, content, end);
    }

    private void loadData() {
        ProtoRequestHelper.task(ProtoHttpFactory.getIncomeInfo()).setTag(this.hashCode()).request(new ProtoCallBack() {
            @Override
            public void onRequestSuccess(String command, String data) {
                Result<ProHistoryViewModel> result = JSON.parseObject(data, new TypeReference<Result<ProHistoryViewModel>>() {
                }.getType());
                if (result.result == CodeSuccess && result.data != null) {
                    initData(result.data);
                }
            }

            @Override
            public void onRequestError(String command, int code, String message) {
            }
        });
    }
}
